import View from "../../../GameFramework/MVC/View";
import UIUtils from "../../../GameFramework/Utils/UIUtils";
import { EPokerStatus } from "../../ConfigESuit";
import UIPoker from "../../View/UIPoker/UIPoker";
import GameDB, { Poker } from "../GameDB";
import GameEvent from "../GameEvents";

const { ccclass, property } = cc._decorator;
@ccclass
export default class GameView extends View {

    @property(cc.Prefab) pokerPrefab: cc.Prefab = null;
    @property(cc.Node) initAreaPokers: cc.Node = null;
    @property(cc.Node) closeSendArea: cc.Node = null;
    @property(cc.Node) openSendArea: cc.Node = null;
    @property([cc.Node]) receiveArea: cc.Node[] = [];
    @property(cc.Node) playGroupRoot: cc.Node = null;

    // private playGroupList: cc.Node[] = [];
    private model: GameDB = null;
    public BindModel(model: GameDB) {
        this.model = model
        this.model.on(GameEvent.INIT_POKER, this.OnEventInit, this)
        this.model.on(GameEvent.PLAY, this.OnEventPlay, this)
        this.model.on(GameEvent.INIT_GROUP_CARD, this.OnEventInitGroupCard, this)
        this.on(GameEvent.POKER_MOVE_PLAYAREA_TO_RESIVEAREA,this.model.OnEventPokerMoveFromPlayToRecive,this.model)
    }

    public UnBindModel() {
        this.model.off(GameEvent.INIT_POKER, this.OnEventInit)
        this.model.off(GameEvent.PLAY, this.OnEventPlay)
        this.model.off(GameEvent.INIT_GROUP_CARD, this.OnEventInitGroupCard)
        this.off(GameEvent.POKER_MOVE_PLAYAREA_TO_RESIVEAREA,this.model.OnEventPokerMoveFromPlayToRecive)

    }

    public onLoad(): void {
        // for (let i = 0; i < GameDB.CONST_PLAY_GROUPS; i++) {

        //     let playGroup = new cc.Node()
        //     playGroup.position = new cc.Vec3(85 * i, 0, 0)
        //     this.playGroupAnchor.addChild(playGroup)
        //     this.playGroupList.push(playGroup)

        // }
    }

    public InitPokers(pokers: Poker[]) {
        //创建扑克牌UI
        pokers.forEach((poker, index) => {
            let uiPoker = this.CreatUIPoker(poker);
            uiPoker.node.x = 0.2 * index;
            uiPoker.node.y = 0.1 * index;
            // uiPoker.node.y=-50*index
            //把UI添加到节点上
            this.initAreaPokers.addChild(uiPoker.node);
        });
    }

    private CreatUIPoker(poker: Poker): UIPoker {
        let uiPokerNode = cc.instantiate(this.pokerPrefab);
        let uiPoker: UIPoker = uiPokerNode.getComponent(UIPoker);
        //初始化
        uiPoker.Init(poker, this);
        // uiPoker.node.setPosition(0,0);

        return uiPoker;
    }

    private OnPlay() {
        //将初始化区域的扑克移动到发牌区
        let stack: cc.Node[] = []
        for (let i = this.initAreaPokers.children.length - 1; i >= 0; --i) {
            let child = this.initAreaPokers.children[i]
            stack.push(child)
            this.initAreaPokers.removeChild(child)
        }
        for (let i = stack.length - 1; i >= 0; --i) {
            let child = stack[i]
            this.closeSendArea.addChild(child)
        }
    }

    public OnEventInit(pokers: Poker[]) {
        console.log("OnEventInit>>");
        this.InitPokers(pokers);
    }
    public OnEventPlay(pokers: Poker[]) {
        console.log("OnEventPlay>>");
        this.OnPlay()
    }
    public OnEventInitGroupCard(groupIndex: number, cardIndex: number, poker: Poker) {
        let index = GameDB.CONST_PLAY_GROUPS * cardIndex - cardIndex * (cardIndex - 1) / 2 - cardIndex + groupIndex
        // console.log("OnEventInitGroupCard>>",groupIndex,cardIndex,poker);
        //先移动UI
        let node = poker.view.node
        UIUtils.move(node, this.playGroupRoot)

        node.zIndex = index
        let delay = index * 0.01
        let px = groupIndex * 85

        if (poker.status == EPokerStatus.OPEN) {
            cc.tween(node)
                .delay(delay)
                .to(0.5, { position: cc.v2(px, -30 * cardIndex) })
                .to(0.3, { scaleX: 0 })
                .call(() => {
                    //ui的显示状态刷新出来
                    poker.view.Refresh()
                })
                .to(0.3, { scaleX: 1 })
                .start()

        } else {
            cc.tween(node)
                .delay(delay)
                .to(0.5, { position: cc.v2(px, -30 * cardIndex) })
                .start()
        }
    }

    public OnClickUIPoker(uiPoker: UIPoker) {


        //1.这张牌如果在玩牌区
        //2.翻开的
        //3.在最上方
        //4.点数是A
        // -->这张牌可以移动到收牌区
        if (this.isLocationPlayArea(uiPoker)) {
            if (uiPoker.isOpen()) {
                if (this.isIndexPlayAreaGroupTop(uiPoker)) {
                    if (uiPoker.isPoint(1)) {
                        console.log("click");
                        this.emit(GameEvent.POKER_MOVE_PLAYAREA_TO_RESIVEAREA, uiPoker.poker)
                    }
                }
            }
        }

    }


    private isIndexPlayAreaGroupTop(uiPoker: UIPoker): boolean {
        return this.model.isIndexPlayAreaGroupTop(uiPoker.poker)

    }

    private isLocationPlayArea(uiPoker: UIPoker): boolean {
        return this.model.isLocationPlayArea(uiPoker.poker)

    }
}
